/*************************************************************************
> File Name: sequential_lookup.c
> Author: icebear
> Mail: 3322366195@qq.com
> Created Time: 2023-07-23 23:20:04
> Description: 顺序查找
************************************************************************/

#include <stdio.h>
int sequentialLookup(int arr[], int length, int target);
int main(int argc, char const *argv[])
{
    int arr[100] = {0};
    for (int i = 0; i < 100; i++)
    {
        arr[i] = i;
    }
    int target = 100;
    int targetSuffix = sequentialLookup(arr, 100, target);
    printf("target %d suffix is %d\n", target, targetSuffix);
    target = 78;
    targetSuffix = sequentialLookup(arr, 100, target);
    printf("target %d suffix is %d\n", target, targetSuffix);
    return 0;
}
/**
 * 查找目标值
 */
int sequentialLookup(int arr[], int length, int target)
{
    // 不在数组范围
    if (arr[0] > target || arr[length - 1] < target)
    {
        return -1;
    }
    int targetSuffix = -1;
    for (int i = 0; i < length; i++)
    {
        if (arr[i] == target)
        {
            targetSuffix = i;
            break;
        }
    }
    return targetSuffix;
}
